<?php

namespace app\controllers\shop;

use app\models\shop\MerchantCategoryGroupModel;
use app\models\shop\MerchantCategoryModel;
use yii;
use yii\web\ShopController;

class CategoryGroupController extends ShopController {

    public $enableCsrfValidation = false; //禁用CSRF令牌验证，可以在基类中设置

    public function behaviors() {
        return [
            'token' => [
                'class' => 'yii\filters\ShopFilter', //调用过滤器
//                'only' => ['single'],//指定控制器应用到哪些动作
                'except' => ['list','category'],//指定控制器不应用到哪些动作
            ]
        ];
    }

    /**
     * 分类分组列表
     */
    public function actionList() {
        if (yii::$app->request->isGet) {
            $request = yii::$app->request; //获取 request 对象
            $params = $request->get(); //获取地址栏参数

            $model = new MerchantCategoryGroupModel();
            $params['status'] = 1;
            $params['limit'] = false;
            $array = $model->do_select($params);
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return $array;

        } else {
            return result(500, "请求方式错误");
        }
    }

    /**
     * 分类分组列表
     */
    public function actionCategory($id) {
        if (yii::$app->request->isGet) {
            $request = yii::$app->request; //获取 request 对象
            $params = $request->get(); //获取地址栏参数

            $model = new MerchantCategoryGroupModel();
            $info = $model->do_one(['id'=>$id]);
            if ($info['status'] != 200) {
                \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
                return $info;
            }
            $categoryModel = new MerchantCategoryModel();
            $categoryId = json_decode($info['data']['category_id']);
            $ids = '';
            if ($categoryId != null){
                for ($i = 0; $i < count($categoryId); $i++){
                    if ($i == 0){
                        $ids .= $categoryId[$i];
                    }else{
                        $ids .= ",$categoryId[$i]";
                    }
                }
                $where["id in ($ids)"] = null;
                $where['status'] = 1;
                $array = $categoryModel->findall($where);
                \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
                return $array;
            } else {
                return result(204, "查询失败");
            }

        } else {
            return result(500, "请求方式错误");
        }
    }

}
